<template>
  <!-- 为什么不要sync？因为到时候它的显示和隐藏是由父组件决定 -->
  <!-- 因为加了sync，会自己在内部改掉这个值，但是这个值不应该自己改，而应该是由父组件改 -->
  <el-dialog title="新增员工" :visible="showDialog" @close="cancelDialog">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      style="width: 75%; margin: 0 auto"
    >
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          style="width: 100%"
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          style="width: 100%"
          v-model="form.formOfEmployment"
        >
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id"></el-option>

        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          @focus="showTreeData"
          @blur="handleBlur"
          v-model="form.departmentName"
          autocomplete="off"
        ></el-input>
        <!-- 放一个tree来显示部门 -->
        <el-tree
          v-if="treeShow"
          v-loading="treeLoading"
          default-expand-all
          :data="departments"
          :props="defaultProps"
          @node-click="nodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          style="width: 100%"
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelDialog">取 消</el-button>
      <el-button type="primary" @click="save"
        >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入获取部门的接口
import { getDepartments } from "@/api/departments";
import { addEmp } from "@/api/employees";
// 导入工具函数
import { transToTreeData } from "@/utils";
// 导入枚举库
import enumObj from '@/api/constant/employees'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      hireType: enumObj.hireType,
      // 表单绑定数据
      form: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        correctionTime: "",
        departmentName: "",
      },
      // 表单规则
      rules: {
        username: [
          { required: true, message: "请输入员工姓名", trigger: "blur" },
        ],

        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        timeOfEntry: [
          { required: true, message: "请选择入职时间", trigger: "blur" },
        ],
        formOfEmployment: [
          { required: true, message: "请选择聘用形式", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "请输入员工工号", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "请选择部门", trigger: "change" },
        ],
        correctionTime: [
          { required: true, message: "请选择转正时间", trigger: "blur" },
        ],
      },

      // 树形控件绑定数据
      departments: [],
      // 指定树的文字和子节点是什么属性
      defaultProps: {
        label: "name",
        children: "children",
      },
      // 控制树形控件是否显示的属性
      treeShow: false,
      treeLoading: false,
    };
  },

  methods: {
    // 部门文本框获得焦点事件
    async showTreeData() {
      // 显示树形
      this.treeShow = true;
      this.treeLoading = true;
      // 加载部门数据
      let res = await getDepartments();
      this.departments = transToTreeData(res.data.depts, "");
      this.treeLoading = false;
    },

    // 参数1：被点的这个节点的数据
    // 参数2: 被点的这个节点对象
    // 参数3: 被点的这个节点所在的tree组件对象
    nodeClick(data, node, tree) {
      // 把被点的节点数据里的name赋值给文本框绑定的数据
      this.form.departmentName = data.name;
      this.treeShow = false;
    },

    handleBlur(e) {
      // 如果失去焦点时去的不是tree，则隐藏tree
      if (e.relatedTarget.className.indexOf("el-tree-node") == -1) {
        this.treeShow = false;
      }
    },

    // 点击确定的点击事件
    save () {

      // 做整个表单验证
      this.$refs.form.validate( async v => {

        if (v) {
          await addEmp(this.form)
          this.$message.success('新增成功！')
          // 隐藏对话框
          this.$emit('update:showDialog', false)
          // 要让父组件加载最后一页的数据
          // 如何得到最后一页？ 用总数量 / 页容量 再向上取整
          // 例如：10条数据，一页10条，就1页， 12条数据，一页10条两页
          let pageInfo = this.$parent.pageInfo
          // 假设当前total为30,page为1 size为10
          // 根据下面的算法 Math.ceil( (30 + 1) / 10 ) 向上取整得到4
          pageInfo.total += 1
          let lastPage = Math.ceil(pageInfo.total / pageInfo.size)
          // 让父组件请求最后一页
          // 得到4以后给了page,page就是4，但是total还是30，size为10，所以根据分页算出来的还是总共只有3页，你给4它也只能到3
          this.$parent.pageInfo.page = lastPage
          // 父组件要刷新列表
          this.$parent.getList()
        }
      } )
    },

    // 取消和关闭的事件
    cancelDialog () {

      this.form = {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        correctionTime: "",
        departmentName: "",
      }

      // 重置表单
      this.$refs.form.resetFields()
      // 隐藏对话框
      this.$emit('update:showDialog', false)
    }
  },
};
</script>

<style>
</style>